package com.zyht.website.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wx.dao.SysRoleMapper;
import com.wx.dao.SysUserRoleMapper;
import com.wx.entity.SysRole;
import com.wx.entity.SysRoleExample;
import com.wx.entity.SysUserRole;
import com.wx.entity.SysUserRoleExample;
import com.wx.exception.BussException;
import com.wx.util.Md5Encrypt;
import com.zyht.website.dao.SysUserMapper;
import com.zyht.website.entity.SysUser;
import com.zyht.website.entity.SysUserExample;
import com.zyht.website.entity.SysUserExample.Criteria;
import com.zyht.website.service.SysUserService;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

	@Resource
	private SysUserMapper sysUserMapper;

	@Resource
	private SysRoleMapper roleMapper;

	@Resource
	private SysUserRoleMapper userRoleMapper;

	@Override
	public int insert(SysUser user){

		return sysUserMapper.insert(user);
	}

	@Override
	public List<SysUser> selectByExample(SysUserExample example){

		return sysUserMapper.selectByExample(example);
	}

	@Override
	public SysUser selectByPrimaryKey(Integer id){

		return sysUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public int countByExample(SysUserExample example){

		return sysUserMapper.countByExample(example);
	}

	@Override
	public int updateByExample(SysUser record,SysUserExample example){

		return sysUserMapper.updateByExample(record,example);
	}

	@Override
	public int updateByPrimaryKey(SysUser record){

		return sysUserMapper.updateByPrimaryKey(record);
	}

	@Override
	public int deleteByPrimaryKey(Integer id){

		return sysUserMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByPrimaryKey(SysUserExample example){

		return sysUserMapper.deleteByExample(example);
	}

	@Override
	public SysUser isPhoneExist(String mobile){

		SysUserExample example = new SysUserExample();
		Criteria cr = example.createCriteria();
		cr.andMobileEqualTo(mobile);
		List<SysUser> list = sysUserMapper.selectByExample(example);
		if(list != null && list.size() == 1){
			return list.get(0);
		} else{
			return null;
		}
	}

	@Override
	public SysUser selectBySysUserName(String username){

		SysUserExample example = new SysUserExample();
		Criteria cr = example.createCriteria();
		cr.andUsernameEqualTo(username);
		List<SysUser> list = sysUserMapper.selectByExample(example);
		if(list != null && list.size() == 1){
			return list.get(0);
		} else{
			return null;
		}
	}

	@Override
	public SysUser selectByMobile(String mobile){

		SysUserExample example = new SysUserExample();
		Criteria cr = example.createCriteria();
		cr.andMobileEqualTo(mobile);
		List<SysUser> list = sysUserMapper.selectByExample(example);
		if(list != null && list.size() == 1){
			return list.get(0);
		} else{
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wx.service.SysUserService#addSysUser(java.lang.String, java.lang.Integer, java.lang.String)
	 */
	@Override
	public int addSysUser(String username,Integer roleId,String status,Integer orgId){

		SysUserExample example = new SysUserExample();
		example.createCriteria().andUsernameEqualTo(username);
		int count = countByExample(example);
		if(count > 0){
			throw new BussException("账号已被占用");
		}

		SysRoleExample example1 = new SysRoleExample();
		example1.createCriteria().andIdEqualTo(roleId).andRoleStatusEqualTo("1");
		int i1 = roleMapper.countByExample(example1);
		if(i1 == 1){} else{
			throw new BussException("角色信息不正确");
		}

		SysUser sub = new SysUser();
		sub.setUsername(username);
		sub.setStatus(status);
		sub.setMobile("");
		sub.setCreateip("127.0.0.1");
		sub.setCreatetime("");
		sub.setEmail("");
		sub.setLastip("127.0.0.1");
		sub.setLasttime("");
		sub.setOrgId(orgId);
		String rePassword = Md5Encrypt.md5("ncb" + username);
		sub.setPassword(rePassword);
		insert(sub);
		SysUserRole record = new SysUserRole();
		record.setRoleId(roleId);
		record.setUserId(sub.getId());
		userRoleMapper.insertSelective(record);
		return 1;
	}

	/**
	 * @see com.wx.service.SysUserService#updateSysUser(java.lang.String, java.lang.Integer, java.lang.String,
	 *      java.lang.Integer)
	 */
	@Override
	public int updateSysUser(String username,Integer roleId,String status,Integer userId,Integer orgId){

		SysUser subs = sysUserMapper.selectByPrimaryKey(userId);
		if(subs == null){
			throw new BussException("用户信息不正确");
		}
		SysUserExample example = new SysUserExample();
		example.createCriteria().andUsernameEqualTo(username).andIdNotEqualTo(userId);
		int count = sysUserMapper.countByExample(example);
		if(count > 0){
			throw new BussException("登录名已存在,请更换其他登录名");
		}

		SysUser sub = new SysUser();
		sub.setUsername(username);
		sub.setStatus(status);
		sub.setId(userId);
		sub.setOrgId(orgId);
		sysUserMapper.updateByPrimaryKeySelective(sub);

		SysUserRoleExample exampleR = new SysUserRoleExample();
		exampleR.createCriteria().andUserIdEqualTo(userId);
		userRoleMapper.deleteByExample(exampleR);

		SysUserRole record = new SysUserRole();
		record.setRoleId(roleId);
		record.setUserId(userId);
		userRoleMapper.insertSelective(record);

		return 0;
	}

	@Override
	public List<SysRole> findRoleByUserId(Integer id){

		SysUserRoleExample urExample = new SysUserRoleExample();
		urExample.createCriteria().andUserIdEqualTo(id);
		List<SysUserRole> userRoles = userRoleMapper.selectByExample(urExample);
		if(userRoles.isEmpty()){
			return null;
		}
		List<Integer> roleids = new ArrayList<>();
		for(SysUserRole ur:userRoles){
			roleids.add(ur.getRoleId());
		}
		SysRoleExample roleExample = new SysRoleExample();
		roleExample.createCriteria().andIdIn(roleids);
		List<SysRole> roles = roleMapper.selectByExample(roleExample);
		return roles;
	}

	@Override
	public List<Integer> findRoleIdByUserId(Integer id){

		List<Integer> roleids = new ArrayList<>();

		SysUserRoleExample urExample = new SysUserRoleExample();
		urExample.createCriteria().andUserIdEqualTo(id);
		List<SysUserRole> userRoles = userRoleMapper.selectByExample(urExample);
		if(userRoles.isEmpty()){
			return roleids;
		}
		for(SysUserRole ur:userRoles){
			roleids.add(ur.getRoleId());
		}
		return roleids;
	}

}
